#ifndef PORTMODEL_H
#define PORTMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QSerialPortInfo>
#include <QWindow>
#include <QMutex>
#include <functional>
#include "serialport/portmonitor.h"

class PortModel : public QAbstractListModel, OnMonitorListener
{
    Q_OBJECT
public:
    using OnPortLoadedListener = std::function<void(QList<QSerialPortInfo>&)>;

private:
    PortMonitor* mpPortMonitor;
    OnPortLoadedListener mOnPortLoadedListener;
    static QHash<int, QByteArray> sRoleNames;
    QList<QSerialPortInfo> mPorts;
    static QString sPortNamePrefix;

public:
    explicit PortModel(QObject *parent = nullptr);
    virtual ~PortModel();

    void startMonitor(WId winId);
    void stopMonitor();

    virtual void onPortArrival(QString port, QSerialPortInfo& info) override;
    virtual void onPortRemoved(QString port, QSerialPortInfo& info) override;

    void setOnPortLoadedListener(OnPortLoadedListener listener);
    QString getPort(int index);

    // QAbstractItemModel interface
public:
    int rowCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QHash<int, QByteArray> roleNames() const override;

private:
    void loadPort();

signals:

};

#endif // PORTMODEL_H
